package com.acraftsman.mr_camera;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.sosocam.ipcammgr.IPCamMgr;
import com.sosocam.util.Wifi;

public class ScanService extends Service {

  private NotificationCompat.Builder mBuilder;

  private static boolean isRunning = false;

  @Override
  public void onCreate() {
    super.onCreate();
    mHandler.sendEmptyMessageDelayed(0, 5000);
    /* 注册屏幕唤醒时的广播 */
    IntentFilter mScreenOnFilter = new IntentFilter("android.intent.action.SCREEN_ON");
    ScanService.this.registerReceiver(mScreenOReceiver, mScreenOnFilter);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //IPCam.hw_decode = IPCam.HW_DECODE_SW_SHOW;
    Wifi.init(this);
    IPCamMgr.init(this);
    System.out.println("服务已经启动");
    String CHANNEL_ONE_ID = "com.acraftsman.mr_camera";
    String CHANNEL_ONE_NAME = "MrCamera";
    if (mBuilder == null) {
      mBuilder = new NotificationCompat.Builder(this, CHANNEL_ONE_ID)
              .setContentText("正在后台运行")
              .setContentTitle("抄表杆服务")
              .setSmallIcon(R.drawable.logo);

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //修改安卓8.1以上系统报错
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
        notificationChannel.enableLights(false);//如果使用中的设备支持通知灯，则说明此通知通道是否应显示灯
        notificationChannel.setShowBadge(false);//是否显示角标
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(notificationChannel);
        mBuilder.setChannelId(CHANNEL_ONE_ID);
      }
      startForeground(1, mBuilder.build());
    }
    isRunning = true;
    return START_STICKY;
  }

  @SuppressLint("HandlerLeak")
  private Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if (mBuilder != null) {
        startForeground(10, mBuilder.build());
      }
      sendEmptyMessageDelayed(0, 3 * 60 * 1000);
    }
  };


  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  private BroadcastReceiver mScreenOReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (action.equals("android.intent.action.SCREEN_ON")) {
        if (IPCamMgr.need_restart()){
          IPCamMgr.restart();
        }
      }
    }
  };

  @Override
  public void onDestroy() {
    unregisterReceiver(mScreenOReceiver);
    Wifi.deinit();
    IPCamMgr.deinit();
    isRunning = false;
    super.onDestroy();
  }

  public static boolean isServiceRunning(){
    return isRunning;
  }

}
